import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_module/widget/login.dart';
import 'package:flutter_module/widget/second.dart';
import 'package:flutter_module/widget/video_list.dart';

void main() => runApp(new MaterialApp(
  home: new Login(),
  routes: <String, WidgetBuilder> {
    '/screen1': (BuildContext context) => new SecondScreen(),
    '/screen2' : (BuildContext context) => new VideoList()
  },
));

class MyApp extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      title: 'Welcome to Flutter',
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Welcome to Flutter'),
        ),
        body: new Center(
          child: new HomePage(),
        ),
      ),
    );
  }

}

//主页要展示的内容
class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState

    return new GridViewState();
  }

}

class GridViewState extends State {

  static const platform = const MethodChannel('samples.flutter.io/battery');

  static const counterPlugin = const EventChannel('samples.flutter.io/battery');

  String dataShared = "No data";

  StreamSubscription _subscription = null;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    //开启监听
    if(_subscription == null){
      _subscription =  counterPlugin.receiveBroadcastStream().listen(_onEvent,onError: _onError);
    }

    getAndroidData();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return buildGrid();
  }

  Widget buildGrid() {
    return new GridView.extent(
        maxCrossAxisExtent: 150.0,
        padding: const EdgeInsets.all(4.0),
        mainAxisSpacing: 4.0,
        crossAxisSpacing: 4.0,
        children: _buildGridTileList(1000));
  }

  List<Container> _buildGridTileList(int count) {

    return new List<Container>.generate(
        count,
            (int index) => _buildItemContainer(index)
    );
  }

  Container _buildItemContainer(int index) {

    return new Container(child: _buildItemGestureDetector(index));
  }

  GestureDetector _buildItemGestureDetector(int index) {

    return GestureDetector(
      onTap: () {
        print("onTap index -> $index");

        Navigator.push(
          context,
          new MaterialPageRoute(builder: (context) => new SecondScreen()),
        );

      },
      //监听更多方法可以在这里添加
      child: Center(
        child: new Image.asset('images/pic.jpg'),
      ),
    );

  }

  getAndroidData() async {
    var sharedData = await platform.invokeMethod("AndroidData");
    print("sharedData -> $sharedData");
    if (sharedData != null) {
      setState(() {
        dataShared = sharedData;
      });
    }
  }

  void _onEvent(Object event) {
    setState(() {
      //_count = event;
      print("ChannelPage: $event");
    });
  }

  void _onError(Object error) {
    setState(() {
      //_count = "计时器异常";
      print(error);
    });
  }
}


